﻿Chenxin.UI.RegionBlockMgr=Chenxin.extend(Chenxin.UI.PageCommon,{
	actionUrl:'/service/RegionBlockMgr.ashx',
	leafItemKey:'System.Block',
	createForm:function(){
		return new Chenxin.UI.RegionBlockForm({
			scope:this,
			saveSucc:function(){
				this.setTreeParam(this.tree.currentNode);
				this.tree.reload(this.tree.currentNode);
			},
			afterGetFormJson:function(model){
				switch(this.tree.currentNode.type){
					case 'city':
						model.ItemKey='System.Region';	
						model.CityId=this.tree.currentNode[this.tree.valField];
						model.City=this.tree.currentNode[this.tree.txtField];

						if(this.form.additionalParam.region){
							delete this.form.additionalParam.region;
						}
						break;
					case 'region':	
						model.ItemKey=this.leafItemKey;//'System.Block';
						model.CityId=this.tree.currentNode.cityid;
						model.City=this.tree.currentNode.city;
						
						this.form.additionalParam.region=this.tree.currentNode.itemindex;
						break;
				}				
			}
		});
	},
	createTree:function(){
		var _this=this;
		return new Chenxin.UI.TreeGrid({
			actionUrl:this.actionUrl,
			width:0,
			renderTo:'rb_tree',
			// title:'区域板块管理',
			showRoot:false,
			param:{type:'city'},
			hasCheckBox:true,
			column:[
				{create:true,width:400,name:'txt',header:'名称'},
				{create:true,width:100,name:'itemkey',header:'标识值'},
				{create:true,width:100,name:'itemtype',header:'标识类型'},
				{create:true,width:100,name:'itemindex',header:'排序'},
				{create:true,width:60,name:'remark',header:'描述'}
			],
			beforeExpand:function(node){
				_this.setTreeParam.call(_this,node);
			},
			root:{
				val:'0',
				txt:'易居臣信'
			}
		});
	},
	initEvent:function(){
		var _this=this;
		$('#btn_addregion').click(function(){
			if(!_this.tree.currentNode){
				alert('请选择一个城市节点！');
				return;
			}
			if(_this.tree.currentNode.type!='city'){
				alert('请选择一个城市节点！');
				return;				
			}
			$('#tr_itemindex').show();
			_this.form.reset();
			_this.add();
			_this.form.setTitle('添加区域');
		});
		$('#btn_addblock').click(function(){
			if(!_this.tree.currentNode){
				alert('请选择一个区域节点！');
				return;
			}
			if(_this.tree.currentNode.type!='region'){
				alert('请选择一个区域节点！');
				return;				
			}
			$('#tr_itemindex').hide();
			_this.form.reset();
			_this.add();
			_this.form.setTitle(_this.leafItemKey=='System.Blcok'?'添加板块':'添加片区');		
		});
		
		$('#btn_del').click(function(){
			if(!_this.tree.currentNode){
				alert('请选择一个非城市节点');
				return;
			}
			if(_this.tree.currentNode.type=='city'){
				alert('请选择一个非城市节点');
				return;
			}
			_this.currentId=_this.tree.currentNode[_this.tree.valField];
			
			var leafName=_this.leafItemKey=='System.Blcok'?'板块':'片区';
			if(_this.tree.currentNode.type=='region'){
				_this.del('','确定要删除这个区域吗？\n此区域所属板块和片区将一并删除！');
			}else{
				_this.del('','确定要删除这个'+leafName+'吗？','',{region:_this.tree.findParentNode(_this.tree.currentNode).itemindex});			
			}
		});
	},
	customerInit:function(){
		this.tree=this.createTree();
	},	
	setTreeParam:function(node){
		switch(node.type){
			case 'city':
				this.tree.param.type='region';
				break;
			case 'region':
				if(this.leafItemKey=='System.Block'){
					this.tree.param.type='block';
				}else{
					this.tree.param.type='area';
				}
				this.tree.param.itemkey=this.leafItemKey;
				this.tree.param.cityid=node.cityid;
				this.tree.param.itemindex=node.itemindex;
				break;
		}
	},
	afterDelSucc:function(data){
		var node=this.tree.findParentNode(this.tree.currentNode);
		this.setTreeParam(node);
		this.tree.reload(node);
			
		delete this.currentId;
		delete this.tree.currentNode;	
	},
	initComponent:function(){
		Chenxin.UI.RegionBlockMgr.superclass.initComponent.call(this);
		
	}
});


/**
*@description 板块添加
*/
Chenxin.UI.RegionBlockForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'/service/RegionBlockMgr.ashx',
	id:'rbwin',
	title:'添加板块',
	renderTo:'jqm_rbwin',
	jqmWidth:400,
	defBBtn:true,
	createForm:function(){
		return Chenxin.UI.FormCommon.regionBlockForm(this.id);
	},
	initEvent:function(){	
	},	
	initFormValidator:function(){
	    //权限编号验证
        $('#'+this.id+'_itemvalue').formValidator({ validatorgroup:this.id,
            onshow: "请输入参数名称", onfocus: "请输入参数名称", oncorrect: " "
        }).inputValidator({
            min: 1, empty: { emptyerror: "参数名称不能为空!" }, onerror: "参数名称不能为空!"
        });
		
        $('#'+this.id+'_itemtype').formValidator({ validatorgroup:this.id,
            onshow: "请输入参数类型", onfocus: "请输入参数类型", oncorrect: " "
        }).inputValidator({
            min: 0, empty: { emptyerror: "请输入正整数!" }, onerror: "请输入正整数!"
        }).functionValidator({
            fun: function(val, elem) {
				return val.isNum();
			},onerror: "请输入数字"
        });
		
		
		
		$('#'+this.id+'_itemindex').formValidator({ validatorgroup:this.id,
            onshow: "请输入排序", onfocus: "请输入排序", oncorrect: " "
        }).inputValidator({
            min: 0, empty: { emptyerror: "请输入正整数!" }, onerror: "请输入正整数!"
        }).functionValidator({
            fun: function(val, elem) {
				return val.isNum();
			},onerror: "请输入数字"
        });
	},
	getItemIndex:function(region){
		if(!region){
			this.itemIndex=undefined;
			return;
		}
	
		var _this=this;
		Chenxin.ajax({
			url:this.actionUrl,
			data:{op:'getitemindex',region:region},
			type:'get',
			onSuccess:function(data,msg){
				_this.itemIndex=data.ItemIndex;
			}
		});
	},
	
	initComponent:function(){
		Chenxin.UI.RegionBlockForm.superclass.initComponent.call(this);
	}
});


/**
*@description 楼栋DIV
*@method 提交按钮的处理方法
*/
Chenxin.UI.FormCommon.regionBlockForm=function(comboIdPrefix){
        return '<table class="tbl_f"><tr><th><span class="isfilled">*</span>名称：</th><td><input type="text" class="inputbox_n fl" id="'+comboIdPrefix+'_itemvalue" name="ItemValue" style="width:120px;"/><div id="'+comboIdPrefix+'_itemvalueTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;" class="fl"></div></td></tr><tr><th>参数类型：</th><td><input type="text" class="inputbox_n fl" name="ItemType" id="'+comboIdPrefix+'_itemtype" style="width:120px;"/><div id="'+comboIdPrefix+'_itemtypeTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;" class="fl"></div></td></tr><tr id="tr_itemindex"><th>排序：</th><td><input id="'+comboIdPrefix+'_itemindex" type="text" class="inputbox_n fl" name="ItemIndex" style="width:120px;"/><div id="'+comboIdPrefix+'_itemindexTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;" class="fl"></div></td></tr></table>'
};
